<?php

include_once APPLICATION_DIR . "/application/model/PRODUCT_DOWNLOAD.php";

class Product_Download extends DataObjects_PRODUCT_DOWNLOAD
{    
	public static function validateInputFromProduct(&$product)
	{
		global $error_msg;

		$product_downloads = array();
		$posted_multi_files = $_FILES["PRODUCT_DOWNLOAD_FILE"];
		foreach($_REQUEST['PRODUCT_DOWNLOAD_ID'] as $i => $id) {
			$product_download = new Product_Download();
			if ($id) {
				$product_download = $product->product_downloads[$id];
				if (!$product_download->IS_URL) {
					$product_download->OLD_FILE = $product_download->FILE;
				}
				if ($_REQUEST['PRODUCT_DOWNLOAD_REMOVED'][$i]) {
					$product_download->REMOVED = 1;
					$product_downloads[] = $product_download;
					continue;
				}
			}
			
			$product_download->NAME = trim($_REQUEST['PRODUCT_DOWNLOAD_NAME'][$i]);
			if ($product_download->ID || $product_download->NAME) {
				$product_download->IS_URL = $_REQUEST['PRODUCT_DOWNLOAD_IS_URL'][$i];
				$product_download->DISABLED = $_REQUEST['PRODUCT_DOWNLOAD_DISABLED'][$i];
				if ($product_download->IS_URL) {
					$product_download->FILE = trim($_REQUEST['PRODUCT_DOWNLOAD_URL'][$i]);
				} else {
					// if exist upload file
					if ($posted_multi_files['name'][$i]) {
						$posted_file = array(
							'name' => $posted_multi_files['name'][$i],
							'tmp_name' => $posted_multi_files['tmp_name'][$i]
						);

						$product_download->FILE = str_replace(array(" ", "-"), "_", $posted_file["name"]);
						$product_download->TMP_FILE = Utils::uploadFile(self::getDir(), $posted_file, '', '');
						if (!$product_download->TMP_FILE) {
							$error_msg->add('_MSG_UPLOAD_FAILED');
						}
					} else {
						if (!$product_download->ID) {
							$error_msg->add('_MSG_UPLOAD_FAILED');
						}
					}
				}

				$product_downloads[] = $product_download;
			}			
		}

		$product->product_downloads = $product_downloads;

        return $error_msg->isNoError();
	}

	public function insert() {
		$tmp = !empty($this->TMP_FILE) ? self::getDir()."/$this->TMP_FILE" : '';
		
		$ret = parent::insert();
		
		if ($tmp) {
			@rename($tmp, $this->getPath());
		}
		
		return $ret;
	}

	public function update() {
		$tmp = !empty($this->TMP_FILE) ? self::getDir()."/$this->TMP_FILE" : '';

		if (!empty($this->OLD_FILE) && ($this->IS_URL || $tmp)) {
			@unlink($this->getPath($this->OLD_FILE));
		}

		if ($tmp) {
			@rename($tmp, $this->getPath());
		}

		return parent::update();
	}

	public function delete() {
		if (!$this->IS_URL) {
			@unlink($this->getPath());
		}

		return parent::delete();
	}

	public static function getList($id_product = 0, $is_url = 0) {
		$product_download = new Product_Download();
		if ($id_product) {
			$product_download->ID_PRODUCT = $id_product;
		}
		if ($is_url) {
			$product_download->IS_URL = $is_url;
		}

		return $product_download->fetchListObject();
	}

	public static function getDir()
	{
		return PRODUCT_DIR.'/download';
	}

	public function getPath($file = '')
	{
		if (!$file) {
			$file = $this->IS_URL ? '' : $this->FILE;
		}
		if ($file) {
			if ($this->ID) {
				return self::getDir()."/{$this->ID}_$file";
			} else {
				return self::getDir()."/$file";
			}
		}
		return '';
	}
}
?>
